package com.yang;

import com.yang.command.GuiOpenCommand;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.*;

public final class YangGUI extends JavaPlugin implements Listener {

    private static YangGUI instance;

    // 私有构造函数，确保外部无法直接实例化
    private YangGUI() {}

    // 获取全局唯一的实例
    public static YangGUI getInstance() {
        if (instance == null) {
            instance = new YangGUI();
        }
        return instance;
    }

    @Override
    public void onEnable() {
        // 注册事件监听器
        Bukkit.getPluginManager().registerEvents(this, this);
        Bukkit.getPluginCommand("guiOpen").setExecutor(new GuiOpenCommand());
    }

    @EventHandler
    public void playerItemHeldEvents(PlayerItemHeldEvent playerItemHeldEvent) {
        Player player = playerItemHeldEvent.getPlayer();
        ItemStack heldItem = player.getInventory().getItemInMainHand();
        playerItemHeldEvent.getPlayer().sendMessage(String.valueOf(heldItem.getType()));
    }

//    @EventHandler
//    public void onPlayerJoin(PlayerJoinEvent event) {
//        Player player = event.getPlayer();
//        openMainMenu(player);
//    }



    // 创建菜单方法
    public void openMainMenu(Player player) {
        Inventory menu = Bukkit.createInventory(null, 54, "主菜单");
        ItemStack pane = new ItemStack(Material.STAINED_GLASS_PANE);
        // 设置边框为玻璃
        for (int i = 0; i < 9; i++) {
            menu.setItem(i, pane);
            menu.setItem(i + 45, pane);
        }

        for (int i = 9; i < 45; i += 9) {
            menu.setItem(i, pane);
            menu.setItem(i + 8, pane);
        }
        // 添加按钮
        ItemStack playerHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
        // 获取SkullMeta
        SkullMeta skullMeta = (SkullMeta) playerHead.getItemMeta();
        List<String> loreList = new ArrayList<>();
        loreList.add("玩家信息:" + player.getName());
        loreList.add(ChatColor.YELLOW + "游戏金币：" + 100.00);
        loreList.add("称号：超级管理员");
        skullMeta.setLore(loreList);
        playerHead.setItemMeta(skullMeta);
        menu.setItem(13, playerHead);
        ItemStack button2 = new ItemStack(Material.GOLDEN_APPLE);
        menu.setItem(20, button2);

        // 打开菜单
        player.openInventory(menu);
    }

//    private static String getPlayerInfo(Player player) {
        // 返回包含玩家信息的字符串
//        return "玩家信息: " + player.getName() + " 金钱: " + getMoney(player) + "称号: " + getTitle(player);
//    }

    private static double getMoney(Player player) {
        // 实现获取金钱的逻辑
        return 0.0;
    }

    private static String getTitle(Player player) {
        // 实现获取称号的逻辑
        return "默认称号";
    }

    // 处理菜单点击事件
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (event.getInventory().getTitle().equals("主菜单")) {
            event.setCancelled(true); // 阻止玩家移动物品

            Player player = (Player) event.getWhoClicked();
            ItemStack clickedItem = event.getCurrentItem();

            if (clickedItem != null) {
                if (clickedItem.getType() == Material.SKULL_ITEM) {
                    player.sendMessage("你点击了菜单中的钻石剑按钮！");
                    // 在这里执行与按钮相关的操作
                } else if (clickedItem.getType() == Material.GOLDEN_APPLE) {
                    player.sendMessage("你点击了菜单中的金苹果按钮！");
                    // 在这里执行与按钮相关的操作
                }
            }
        }
    }
}
